<template>
  <div class="accedit-container">
        <div class="position">
          <span>当前位置：</span>
<el-breadcrumb separator-class="el-icon-arrow-right">
  <el-breadcrumb-item>个人账号修改</el-breadcrumb-item>
  <el-breadcrumb-item>密码修改</el-breadcrumb-item>
</el-breadcrumb>
    </div>
    <div class="formedit">
          <div class="hwform">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="原密码" prop="origin">
          <el-input v-model="ruleForm.origin"></el-input>
        </el-form-item>
                <el-form-item label="新密码" prop="current">
          <el-input v-model="ruleForm.current"></el-input>
        </el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >立即修改</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form>
    </div>
    </div>
  </div>
</template>

<script>
import http from '@/api/teacherapi'
export default {
  name: '',
  data () {
    return {
      ruleForm: {
        origin: "",
        current:""
      },
      password:"",
      username:"",
      rules: {
        origin: [
          { required: true, message: "请输入密码", trigger: "blur" },
        ],
        current: [
          { required: true, message: "请输入密码", trigger: "blur" },
        ]
      },
    }
  },
  methods: {
        submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if(this.password==this.ruleForm.origin){
              http.edittchacc({username:this.username,origin:this.ruleForm.origin,current:this.ruleForm.current})
              .then((success)=>{
                if(success.status==200){
                  this.$Toast({
                    content:success.data.msg,
                    type:success.data.type
                  })
                  //通知父组件重新渲染页面
                  this.$emit('changekey');
                }
              },(error)=>{
                return Promise.reject(new Error("获取服务器数据失败"))
              })
            }
            //原密码输入错误
            else{
              this.$Toast({
                content:"原密码错误，请仔细回忆！",
                type:"error"
              })
            }
          } else {
            this.$Toast({
              content:"密码不能为空！",
              type:"error"
            })
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
  },
  async mounted () {
    this.username=localStorage.getItem("username");
    let res=await http.gettchaccpwd(this.username);
    if(res.status==200){
      this.password=res.data.password;
    }

  }
}
</script>

<style lang="less" scoped>
.position {
  height: 30px;
  line-height: 30px;
  .el-breadcrumb {
  display: inline-block;
}
  &>span {
    font-size: 16px;
  }
}
.formedit {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 180px;
  .hwform {
    text-align: center;
  }
}
</style>
